package com.yztc.ui.home;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.yztc.config.Url;
import com.yztc.helper.DividerItemDecoration;
import com.yztc.helper.GlideImageLoader;
import com.yztc.helper.OkHttpClientHelper;
import com.yztc.okbuy.R;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by My on 2016/12/19.
 */

public class HomeFragment extends Fragment {
    private Context mContext;
    private List<HomeBean.ResultsBean> list;
    private HomeAdapter adapter;
    private int flog = 0;
    private List<WelcomeBean.ResultsBean> welcomeList;
    private List<String> imageList = new ArrayList<>();

    @Bind(R.id.banner_fragment_home)
    Banner bannerFragmentHome;
    @Bind(R.id.fragment_home_recyclerView)
    RecyclerView fragmentHomeRecyclerView;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    bannerFragmentHome.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
                    //下载图片
                    bannerFragmentHome.setImageLoader(new GlideImageLoader());
                    //设置图片集合
                    bannerFragmentHome.setImages(imageList);
                    //设置标题集合（当banner样式有显示title时）
//                    bannerFragmentHome.setBannerTitles(title);
                    //设置自动轮播，默认为true
                    bannerFragmentHome.isAutoPlay(true);
                    //设置轮播时间
                    bannerFragmentHome.setDelayTime(1500);
                    //设置指示器位置（当banner模式中有指示器时）

                    bannerFragmentHome.setIndicatorGravity(BannerConfig.CENTER);
                    //banner设置方法全部调用完毕时最后调用
                    bannerFragmentHome.start();

                    break;
                case 1:
                    list = (List<HomeBean.ResultsBean>) msg.obj;
                    if (flog == 1) {
                        adapter.reloadRecyclerView(list, true);
                    } else {
                        adapter.reloadRecyclerView(list, false);
                    }

                    break;
            }
        }
    };

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);

        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        list = new ArrayList<>();
        fragmentHomeRecyclerView.setHasFixedSize(true);
        fragmentHomeRecyclerView.addItemDecoration(new DividerItemDecoration(mContext, 1));
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        fragmentHomeRecyclerView.setLayoutManager(manager);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
//        Log.i("aaaa","滴滴！！");
        //轮播图解析
        BannerJson();
        //内容解析
        HttpJson(Url.HOME_CONTENT);
        adapter = new HomeAdapter(mContext, list);
        fragmentHomeRecyclerView.setAdapter(adapter);
    }

    private void BannerJson() {
        new Thread(new Runnable() {


            @Override
            public void run() {
                try {
                    String path = OkHttpClientHelper.getStringFromURL(mContext, Url.HOME_GG, 1);
                    Gson gson = new Gson();
                    WelcomeBean bean = gson.fromJson(path, WelcomeBean.class);
                    welcomeList = bean.getResults();
                    initdeta();
                    handler.sendEmptyMessageAtTime(0, 500);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void initdeta() {
        for (int i = 1; i < welcomeList.size(); i++) {
            imageList.add(welcomeList.get(i).getFocus_image());
        }
    }

    private void HttpJson(final String path) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    String run = OkHttpClientHelper.getStringFromURL(mContext, path, 1);
                    Gson gson = new Gson();
                    HomeBean home = gson.fromJson(run, HomeBean.class);
                    list = home.getResults();
                    Message msg = handler.obtainMessage();
                    msg.what = 1;
                    msg.obj = list;
                    handler.sendMessage(msg);

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
